package com.haust.community.config;

import com.haust.community.interceptor.DataInterceptor;
import com.haust.community.interceptor.LoginRequiredInterceptor;
import com.haust.community.interceptor.LoginTicketInterceptor;
import com.haust.community.interceptor.MessageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Auther: csp1999
 * @Date: 2020/11/24/20:53
 * @Description: 拦截器配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginTicketInterceptor loginTicketInterceptor;

    // 换用Spring Security 替代登录拦截
    @Autowired
    private LoginRequiredInterceptor loginRequiredInterceptor;

    @Autowired
    private MessageInterceptor messageInterceptor;

    @Autowired
    private DataInterceptor dataInterceptor;

    /**
     * 拦截器的注册
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 登录凭证拦截器
        registry.addInterceptor(loginTicketInterceptor)
                // 除了静态资源不拦截,其他都拦截
                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");

//        登录请求拦截器(使用Spring Security 替代登录拦截)
//        registry.addInterceptor(loginRequiredInterceptor)
//        除了静态资源不拦截,其他都拦截
//                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");

        // 消息总数量拦截器
        registry.addInterceptor(messageInterceptor)
                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");

        // 访客统计拦截器
        registry.addInterceptor(dataInterceptor)
                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");
    }
}
